﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;
using IP.Core.IPCommon;

namespace QuanLyBanHang.GiaoDich
{
	public partial class F200_Nhap_so_du_ban_dau : Form
	{
		public F200_Nhap_so_du_ban_dau()
		{
			InitializeComponent();
			set_define_events();
		}

		#region Members
		BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
		#endregion

		#region Private Methods
		private void load_data_to_grid()
		{
			var v_lst_so_du = db.GD_SO_DU
								.Where(x => x.DM_SAN_PHAM.TEN_SAN_PHAM.Contains(m_txt_tu_khoa_tim_kiem.Text)
											|| m_txt_tu_khoa_tim_kiem.Text.Equals(""))
								.GroupBy(x => x.ID_SAN_PHAM )
								.Select(x=>x.OrderByDescending(y=>y.NGAY_THANG).FirstOrDefault())
								.OrderByDescending(x=>x.NGAY_THANG)
								.Select(x => new
								{
									ID = x.ID,
									TEN_SAN_PHAM = x.DM_SAN_PHAM.TEN_SAN_PHAM,
									NGAY_THANG = x.NGAY_THANG,
									SO_LUONG = x.SO_LUONG,
									DON_VI_TINH = x.DM_SAN_PHAM.DON_VI_TINH
								})

								.ToList();
			m_grv.DataSource = v_lst_so_du;
			m_grv.Columns[0].Visible = false;
			m_grv.Columns[1].HeaderText = "Tên sản phẩm";
            m_grv.Columns[1].Width = 220;
			m_grv.Columns[2].HeaderText = "Ngày nhập";
			m_grv.Columns[3].HeaderText = "Số lượng";
            m_grv.Columns[3].Width = 80;
			m_grv.Columns[4].HeaderText = "Đơn vị tính";
            m_grv.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		}
		private void load_data_to_listbox_san_pham()
		{
			var v_lst_san_pham = db.DM_SAN_PHAM
				.Where(x => x.MA_SAN_PHAM.Contains(m_txt_loc_san_pham.Text)
							|| x.TEN_SAN_PHAM.Contains(m_txt_loc_san_pham.Text)
							|| m_txt_loc_san_pham.Text.Equals(""))
				.Select(x => new
				{
					ID = x.ID,
					TEN_SAN_PHAM = x.TEN_SAN_PHAM
				})
				.ToList();
			m_lsb_san_pham.ValueMember = "ID";
			m_lsb_san_pham.DisplayMember = "TEN_SAN_PHAM";
			m_lsb_san_pham.DataSource = v_lst_san_pham;
		}
		private bool check_validate_data_is_ok()
		{
			if (!CIPConvert.is_valid_number(m_txt_so_luong.Text))
			{
				MessageBox.Show("Bạn phải nhập số!", "Thông báo");
				m_txt_so_luong.Focus();
				return false;
			}
			if (string.IsNullOrWhiteSpace(m_txt_don_vi_tinh.Text))
			{
				MessageBox.Show("Bạn phải nhập Đơn vị tính nhỏ nhất!", "Thông báo");
				return false;
			}
			return true;
		}
		private void insert_data()
		{
			if (!check_validate_data_is_ok()) return;
			decimal v_dc_id_san_pham = CIPConvert.ToDecimal(m_lsb_san_pham.SelectedValue);
			GD_SO_DU v_gd = new GD_SO_DU();
			v_gd.ID_SAN_PHAM = v_dc_id_san_pham;
			v_gd.NGAY_THANG = DateTime.Now;
			v_gd.SO_LUONG = Convert.ToInt16(m_txt_so_luong.Text);
			db.GD_SO_DU.Add(v_gd);
			db.SaveChanges();

			//Update don vi tinh
			DM_SAN_PHAM v_dm_san_pham = db.DM_SAN_PHAM.FirstOrDefault(x => x.ID == v_dc_id_san_pham);
			if (v_dm_san_pham != null)
			{
				v_dm_san_pham.DON_VI_TINH = m_txt_don_vi_tinh.Text;
				db.SaveChanges();
			}
		}
		private void update_data()
		{
			if (!check_validate_data_is_ok()) return;
			decimal v_dc_id_san_pham = CIPConvert.ToDecimal(m_lsb_san_pham.SelectedValue);
			decimal v_dc_id_giao_dich = CIPConvert.ToDecimal(m_grv.CurrentRow.Cells[0].Value);
			var v_gd = db.GD_SO_DU.FirstOrDefault(x => x.ID == v_dc_id_giao_dich);
			if (v_gd != null)
			{
				v_gd.ID_SAN_PHAM = v_dc_id_san_pham;
				v_gd.NGAY_THANG = DateTime.Now;
				v_gd.SO_LUONG = Convert.ToInt16(m_txt_so_luong.Text);
				v_gd.DM_SAN_PHAM.DON_VI_TINH = m_txt_don_vi_tinh.Text;
				db.SaveChanges();
			}

			//Update don vi tinh
			DM_SAN_PHAM v_dm_san_pham = db.DM_SAN_PHAM.FirstOrDefault(x => x.ID == v_dc_id_san_pham);
			if (v_dm_san_pham != null)
			{
				v_dm_san_pham.DON_VI_TINH = m_txt_don_vi_tinh.Text;
				db.SaveChanges();
			}
		}
		private void set_define_events()
		{
			this.m_cmd_cap_nhat.Click += m_cmd_cap_nhat_Click;
			this.m_cmd_quay_lai.Click += m_cmd_quay_lai_Click;
			this.m_cmd_them.Click += m_cmd_them_Click;
			this.m_cmd_tim_kiem.Click += m_cmd_tim_kiem_Click;
			this.m_cmd_xoa_trang.Click += m_cmd_xoa_trang_Click;
			this.m_txt_loc_san_pham.TextChanged += m_txt_loc_san_pham_TextChanged;
			this.Load += F200_Nhap_so_du_ban_dau_Load;
			this.m_grv.CellClick += m_grv_CellClick;
			this.m_lsb_san_pham.SelectedIndexChanged += m_lsb_san_pham_SelectedIndexChanged;
		}

		#endregion

		#region Events
		void m_grv_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				decimal v_dc_id_giao_dich = CIPConvert.ToDecimal(m_grv.CurrentRow.Cells[0].Value);
				m_cmd_cap_nhat.Visible = true;
				m_cmd_them.Visible = false;
				var v_gd_giao_dich = db.GD_SO_DU.FirstOrDefault(x => x.ID == v_dc_id_giao_dich);
				if (v_gd_giao_dich != null)
				{
					m_lsb_san_pham.Text = v_gd_giao_dich.DM_SAN_PHAM.TEN_SAN_PHAM;
					m_txt_don_vi_tinh.Text = v_gd_giao_dich.DM_SAN_PHAM.DON_VI_TINH;
					m_txt_so_luong.Text = v_gd_giao_dich.SO_LUONG.ToString();
				}
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void F200_Nhap_so_du_ban_dau_Load(object sender, EventArgs e)
		{
			try
			{
				load_data_to_listbox_san_pham();
				load_data_to_grid();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_txt_loc_san_pham_TextChanged(object sender, EventArgs e)
		{
			try
			{
				load_data_to_listbox_san_pham();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_cmd_xoa_trang_Click(object sender, EventArgs e)
		{
			try
			{
				m_txt_don_vi_tinh.Text = "";
				m_txt_so_luong.Text = "0";
				m_cmd_cap_nhat.Visible = false;
				m_cmd_them.Visible = true;
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_cmd_tim_kiem_Click(object sender, EventArgs e)
		{
			try
			{
				load_data_to_grid();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_cmd_them_Click(object sender, EventArgs e)
		{
			try
			{
				insert_data();
				m_cmd_xoa_trang_Click(null, null);
				load_data_to_grid();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}


		void m_cmd_quay_lai_Click(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void m_cmd_cap_nhat_Click(object sender, EventArgs e)
		{
			try
			{
				update_data();
				m_cmd_xoa_trang_Click(null, null);
				load_data_to_grid();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}
		void m_lsb_san_pham_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				decimal v_dc_id_san_pham = CIPConvert.ToDecimal(m_lsb_san_pham.SelectedValue);
				var v_gd_san_pham = db.DM_SAN_PHAM.FirstOrDefault(x => x.ID == v_dc_id_san_pham);
				m_txt_don_vi_tinh.Text = v_gd_san_pham.DON_VI_TINH;
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}


		#endregion
	}
}
